package com.itcast.cn.conversation.service;

import com.itcast.cn.conversation.mapper.UserMapper;
import com.itcast.cn.conversation.pojo.User;
import com.itcast.cn.jsp.mapper.BrandMapper;
import com.itcast.cn.login.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * <p>类描述</p>
 *
 * @author hyatt 2022/1/24 16:22
 * @version 1.0
 */
public class UserService {
    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    /**
     * 查询用户
     * @param username 用户名
     * @param password 密码
     * @return 用户
     */
    public User select(String username, String password) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.select(username, password);
        sqlSession.close();
        return user;
    }

    /**
     * 添加用户
     * @param user 用户
     */
    public void add(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.add(user);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 查询用户
     * @param username
     * @return
     */
    public User selectByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectByUsername(username);
        sqlSession.close();
        return user;
    }

}
